package com.fuze.controller;

import com.fuze.aspect.anno.AuthAdmin;
import com.fuze.common.Advice;
import com.fuze.common.CustomException;
import com.fuze.common.Result;
import com.fuze.domain.dto.UserDTO;
import com.fuze.domain.vo.UserQueryVO;
import com.fuze.domain.vo.UserVO;
import com.fuze.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * @author sepc_
 * @date 4/13/2021 13:15
 */
@Api(tags = "用户接口")
@RequiredArgsConstructor
@RestController
@Slf4j
public class UserController {

    final UserService userService;

    /**
     * 注入这个request对象的目的在于：获取请求的token用于权限校验
     */
    final HttpServletRequest request;

    /**
     * 分页查询用户
     *
     * @param userQueryVO 查询用户的条件
     * @return 查询结果
     */
    @AuthAdmin
    @ApiOperation("用户分页查询")
    @GetMapping("/user/listUserForPage")
    public Result listUserForPage(UserQueryVO userQueryVO) {
        Integer pageNum = userQueryVO.getPageNum();
        Integer pageSize = userQueryVO.getPageSize();
        log.info("pageNum = {},pageSize = {}", pageNum, pageSize);
        return Result.ok(userService.listUserForPage(userQueryVO));
    }

    /**
     * 根据id查询用户
     *
     * @param userId 用户id
     * @return 统一返回结果
     */
    @AuthAdmin
    @ApiOperation(value = "根据用户id查询用户")
    @GetMapping("/user/findUserById/{userId:\\d+}")
    public Result findUserById(@PathVariable(value = "userId") @Validated @NotNull(message = "用户ID不能为空") Integer userId) {
        return Result.ok(userService.findUserById(userId));
    }

    /**
     * 用户注册
     *
     * @param userVO 用户的视图数据
     * @return 统一返回结果
     */
    @AuthAdmin
    @ApiOperation(value = "添加用户（用户的注册）")
    @PostMapping(value = "/user/register",consumes = "application/json")
    public Result register(@Validated @RequestBody UserVO userVO) {
        log.debug("UserVO = {}", userVO);
        Boolean exist = userService.existByUsername(userVO.getUsername());
        if (exist) {
            throw new CustomException(Advice.REPEAT_REGISTRATION);
        }
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userVO, userDTO);
        int res = userService.register(userDTO);
        if (res != 1) {
            throw new CustomException(Advice.REGISTRATION_FAILED);
        }
        return Result.ok();
    }

    /**
     * 用户的更新接口
     *
     * @param userDTO 用户的数据传输对象
     * @return 统一返回结果
     */
    @ApiIgnore
    @ApiOperation(value = "更新用户")
    @PostMapping("/user/update")
    public Result update(@RequestBody UserDTO userDTO) {
        if (userDTO.getId() == null) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        int update = userService.update(userDTO);
        if (update != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }

    /**
     * 根据id删除用户
     *
     * @param userIds 用户的id数组
     * @return 统一返回结果
     */
    @ApiOperation(value = "删除用户")
    @AuthAdmin
    @DeleteMapping("/user/deleteByIds/{userIds:\\d+}")
    public Result deleteByIds(@PathVariable @Validated @NotNull(message = "用户的Id不能为空") Integer[] userIds) {
        int res = userService.deleteByIds(userIds);
        if (res <= 0) {
            throw new CustomException(Advice.FAILED_DELETED);
        }
        return Result.ok(res);
    }
}
